/***************************************************************
 *
 * (c) 2008 Markus Dittrich 
 *
 * This program is free software; you can redistribute it 
 * and/or modify it under the terms of the GNU General Public 
 * License Version 3 as published by the Free Software Foundation. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License Version 3 for more details.
 *
 * You should have received a copy of the GNU General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA.
 *
 ****************************************************************/

/* qt includes */
#include <QSizeF>

/* STL include */

/* local includes */
#include "data_label_manager.h"

/* import names into namespace */
using std::list;
using std::map;



//-----------------------------------------------------------------
// constructor
//-----------------------------------------------------------------
DataLabelManager::DataLabelManager() 
  :
  MainLabel_(EmptyString_),
  MainLabelPos_(Origin_),
  MainLabelDefaultPos_(Origin_),
  XAxisLabel_(EmptyString_),
  XAxisLabelPos_(Origin_),
  XAxisLabelDefaultPos_(Origin_),
  YAxisLabel_(EmptyString_),
  YAxisLabelPos_(Origin_),
  YAxisLabelDefaultPos_(Origin_),
  DataLabelsPos_(Origin_),
  DataLabelsDefaultPos_(Origin_)
{}


/*****************************************************************
 *
 * public member functions
 *
 *****************************************************************/

//-----------------------------------------------------------------
// assigns a QString to a label. 
//-----------------------------------------------------------------
void DataLabelManager::set_label_name(int label, QString aLabelName)
{
  switch(label)
  {
    case MAIN_LABEL:
      MainLabel_ = aLabelName;
      break;
    case X_AXIS_LABEL:
      XAxisLabel_ = aLabelName;
      break;
    case Y_AXIS_LABEL:
      YAxisLabel_ = aLabelName;
      break;
    default:
      break;
  }
}


//-----------------------------------------------------------------
// assigns a QString as name to a data set. 
//-----------------------------------------------------------------
void DataLabelManager::set_data_label_name(QString dataSetName, 
    QString aLabelName)
{
  DataLabels_[dataSetName] = aLabelName;
}



//-----------------------------------------------------------------
// sets the position of the main label on the canvas. 
//-----------------------------------------------------------------
void DataLabelManager::set_label_pos(int label, QPoint newPosition,
    bool setAsDefault)
{
  switch(label)
  {
    case MAIN_LABEL:
      MainLabelPos_ = newPosition;
      if ( setAsDefault )
      {
        MainLabelDefaultPos_ = newPosition;
      }
      break;
    case X_AXIS_LABEL:
      XAxisLabelPos_ = newPosition;
      if ( setAsDefault )
      {
        XAxisLabelDefaultPos_ = newPosition;
      }
      break;
    case Y_AXIS_LABEL:
      YAxisLabelPos_ = newPosition;
      if ( setAsDefault )
      {
        YAxisLabelDefaultPos_ = newPosition;
      }
      break;
    case DATA_LABEL:
      DataLabelsPos_ = newPosition;
      if ( setAsDefault )
      {
        DataLabelsDefaultPos_ = newPosition;
      }
      break;
    default:
      break;
  }
}


//-----------------------------------------------------------------
// returns the axis label string 
//-----------------------------------------------------------------
const QString& DataLabelManager::get_axis_label(int label) const
{
  switch(label)
  {
    case MAIN_LABEL:
      return MainLabel_;
      break;
    case X_AXIS_LABEL:
      return XAxisLabel_;
      break;
    case Y_AXIS_LABEL:
      return YAxisLabel_;
      break;
    default:
      break;
  }

  /* we should never get here; return empty string */
  return EmptyString_;
}


//-----------------------------------------------------------------
// returns the data label strings 
//-----------------------------------------------------------------
const map<QString,QString>& DataLabelManager::get_data_labels() const
{
  return DataLabels_;
}


//-----------------------------------------------------------------
// returns the current label for a certain data set
//-----------------------------------------------------------------
QString DataLabelManager::get_data_label(QString dataName) 
{
  if ( DataLabels_.find(dataName) != DataLabels_.end() )
  {
    return DataLabels_[dataName];
  }
  else
  {
    return QString("");
  }
}



//-----------------------------------------------------------------
// returns the axis label position 
//-----------------------------------------------------------------
const QPoint& DataLabelManager::get_label_pos(int label) const
{
  switch(label)
  {
    case MAIN_LABEL:
      return MainLabelPos_;
      break;
    case X_AXIS_LABEL:
      return XAxisLabelPos_;
      break;
    case Y_AXIS_LABEL:
      return YAxisLabelPos_;
      break;
    case DATA_LABEL:
      return DataLabelsPos_;
      break;
    default:
      break;
  }

  /* we should never get here; return origin */
  return Origin_;
}


//-----------------------------------------------------------------
// move specified label to the left on canvas
//-----------------------------------------------------------------
void DataLabelManager::move_label_left(int label)
{
  switch(label)
  {
    case MAIN_LABEL:
      MainLabelPos_ += QPoint(-LabelShiftSize_,0);
      break;
    case X_AXIS_LABEL:
      XAxisLabelPos_ += QPoint(-LabelShiftSize_,0);
      break;
    case Y_AXIS_LABEL:
      YAxisLabelPos_ += QPoint(-LabelShiftSize_,0);
      break;
    case DATA_LABEL:
      DataLabelsPos_ += QPoint(-LabelShiftSize_,0);
      break;
    default:
      break;
  }
}


//-----------------------------------------------------------------
// move specified label to the right on canvas
//-----------------------------------------------------------------
void DataLabelManager::move_label_right(int label)
{
  switch(label)
  {
    case MAIN_LABEL:
      MainLabelPos_ += QPoint(LabelShiftSize_,0);
      break;
    case X_AXIS_LABEL:
      XAxisLabelPos_ += QPoint(LabelShiftSize_,0);
      break;
    case Y_AXIS_LABEL:
      YAxisLabelPos_ += QPoint(LabelShiftSize_,0);
      break;
    case DATA_LABEL:
      DataLabelsPos_ += QPoint(LabelShiftSize_,0);
      break;
    default:
      break;
  }
}


//-----------------------------------------------------------------
// move specified label up on canvas
//-----------------------------------------------------------------
void DataLabelManager::move_label_up(int label)
{
  switch(label)
  {
    case MAIN_LABEL:
      MainLabelPos_ += QPoint(0,-LabelShiftSize_);
      break;
    case X_AXIS_LABEL:
      XAxisLabelPos_ += QPoint(0,-LabelShiftSize_);
      break;
    case Y_AXIS_LABEL:
      YAxisLabelPos_ += QPoint(0,-LabelShiftSize_);
      break;
    case DATA_LABEL:
      DataLabelsPos_ += QPoint(0,-LabelShiftSize_);
      break;
    default:
      break;
  }
}


//-----------------------------------------------------------------
// move specified label down on canvas
//-----------------------------------------------------------------
void DataLabelManager::move_label_down(int label)
{
  switch(label)
  {
    case MAIN_LABEL:
      MainLabelPos_ += QPoint(0,LabelShiftSize_);
      break;
    case X_AXIS_LABEL:
      XAxisLabelPos_ += QPoint(0,LabelShiftSize_);
      break;
    case Y_AXIS_LABEL:
      YAxisLabelPos_ += QPoint(0,LabelShiftSize_);
      break;
    case DATA_LABEL:
      DataLabelsPos_ += QPoint(0,LabelShiftSize_);
      break;
    default:
      break;
  }
}


//-----------------------------------------------------------------
// reset specified label to its default
//-----------------------------------------------------------------
void DataLabelManager::reset_label(int label)
{
  switch(label)
  {
    case MAIN_LABEL:
      MainLabelPos_ = MainLabelDefaultPos_; 
      break;
    case X_AXIS_LABEL:
      XAxisLabelPos_ = XAxisLabelDefaultPos_;
      break;
    case Y_AXIS_LABEL:
      YAxisLabelPos_ = YAxisLabelDefaultPos_;
      break;
    case DATA_LABEL:
      DataLabelsPos_ = DataLabelsDefaultPos_;
      break;
    default:
      break;
  }
}


//-----------------------------------------------------------------
// adds a data set name to the list of tracked sets
//-----------------------------------------------------------------
void DataLabelManager::add_data_set(QString aName)
{
  DataLabels_[aName] = QString("");
  DataNames_.push_back(aName);
}


//-----------------------------------------------------------------
// remove a data set name from the list of tracked sets
//-----------------------------------------------------------------
void DataLabelManager::remove_data_set(QString aName)
{
  DataLabels_.erase(aName);
  DataNames_.remove(aName);
}


//-----------------------------------------------------------------
// return a vector with available data set names
//-----------------------------------------------------------------
const list<QString>& DataLabelManager::get_data_label_names() const
{
  return DataNames_;
}


/*****************************************************************
 *
 * initialize static data members
 *
 *****************************************************************/
const QString DataLabelManager::EmptyString_ = QString("");
const QPoint DataLabelManager::Origin_ = QPoint(0,0);

